package com.csap.framework.integration;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import lombok.Data;

import java.util.Properties;

/**
 * rocketmq 配置
 *
 * @author ycf
 */
@Data
public class RocketmqConfig extends RocketmqBaseConfig {
    public static final String prefix = "csap.aliyun.rocketmq";

    /**
     * 消费者配置
     */
    private Consumer consumer;
    /**
     * 生产者配置
     */
    private Producer producer;

    public Consumer getConsumer() {
        if (consumer == null) {
            Consumer consumer = new Consumer();
            consumer.setAccessKey(getAccessKey());
            consumer.setSecretKey(getSecretKey());
            consumer.setNameServiceAddress(getNameServiceAddress());
            return this.consumer = consumer;
        } else {
            if (StringUtils.isNotEmpty(getAccessKey()) && StringUtils.isEmpty(consumer.getAccessKey())) {
                consumer.setAccessKey(getAccessKey());
            }
            if (StringUtils.isNotEmpty(getSecretKey()) && StringUtils.isEmpty(consumer.getSecretKey())) {
                consumer.setSecretKey(getSecretKey());
            }
            if (StringUtils.isNotEmpty(getNameServiceAddress()) && StringUtils.isEmpty(consumer.getNameServiceAddress())) {
                consumer.setNameServiceAddress(getNameServiceAddress());
            }
            return consumer;
        }
    }


    public Producer getProducer() {
        if (producer == null) {
            Producer producer = new Producer();
            producer.setAccessKey(getAccessKey());
            producer.setSecretKey(getSecretKey());
            producer.setNameServiceAddress(getNameServiceAddress());
            return this.producer = producer;
        } else {
            if (StringUtils.isNotEmpty(getAccessKey()) && StringUtils.isEmpty(producer.getAccessKey())) {
                producer.setAccessKey(getAccessKey());
            }
            if (StringUtils.isNotEmpty(getSecretKey()) && StringUtils.isEmpty(producer.getSecretKey())) {
                producer.setSecretKey(getSecretKey());
            }
            if (StringUtils.isNotEmpty(getNameServiceAddress()) && StringUtils.isEmpty(producer.getNameServiceAddress())) {
                producer.setNameServiceAddress(getNameServiceAddress());
            }
            return producer;
        }
    }

    /**
     * 获取消费者属性
     *
     * @return
     */
    public Properties getConsumerProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, getConsumer() == null || StringUtils.isEmpty(getConsumer().getAccessKey()) ? getAccessKey() : getConsumer().getAccessKey());
        properties.setProperty(PropertyKeyConst.SecretKey, getConsumer() == null || StringUtils.isEmpty(getConsumer().getSecretKey()) ? getSecretKey() : getConsumer().getSecretKey());
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, getConsumer() == null || StringUtils.isEmpty(getConsumer().getNameServiceAddress()) ? getNameServiceAddress() : getConsumer().getNameServiceAddress());
        return properties;
    }

    /**
     * 获取生产者属性
     *
     * @return
     */
    public Properties getProducerProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, getProducer() == null || StringUtils.isEmpty(getProducer().getAccessKey()) ? getAccessKey() : getProducer().getAccessKey());
        properties.setProperty(PropertyKeyConst.SecretKey, getProducer() == null || StringUtils.isEmpty(getProducer().getSecretKey()) ? getSecretKey() : getProducer().getSecretKey());
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, getProducer() == null || StringUtils.isEmpty(getProducer().getNameServiceAddress()) ? getNameServiceAddress() : getProducer().getNameServiceAddress());

        return properties;
    }

    /**
     * 消费者
     */
    @Data
    public static class Consumer extends RocketmqBaseConfig {
        /**
         * 接受的消息类型
         */
        private MessageType messageType = MessageType.JSON;
        /**
         * 消息返回选项
         */
        private MessageAction messageAction = MessageAction.SUCCESS;
        /**
         * 消费者 对生产者消息过滤类型
         */
        private MessageFilterType producerMessageFilterType = MessageFilterType.TAG;
        /**
         * 消费者 消息过滤类型
         */
        private MessageFilterType consumerMessageFilterType = MessageFilterType.TOPIC;
        /**
         * 是否开启内置消息过滤
         */
        private Boolean enabledMessageFilter = Boolean.FALSE;
        /**
         * 消费线程数量
         */
        private Integer consumeThreadNums = 20;
        /**
         * 分组ID
         */
        private String groupId;

        /**
         * 订阅主题
         */
        private String topic;
        /**
         * 默认标签
         */
        private String tag = "*";
        /**
         * 是否开启
         */
        private boolean enabled;


    }

    /**
     * 生产者
     */
    @Data
    public static class Producer extends RocketmqBaseConfig {
        /**
         * 是否开启
         */
        private boolean enabled;
    }
}
